//
//  TF_LivingInfo.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/1.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingInfo: TF_TopRightOrCornerTopLeft {
    @objc public let avatar = UIButton()
    @objc public let nickName = UITextField()
    //保存编辑前的名字
    @objc public var tempNickName:NSString = ""
    //编辑
    @objc public let edit = TalkfunIncreaseButton()
    @objc public let label = UILabel()
    @objc public let line = UIView()
    @objc public let content = UIView()
    @objc public let listView = UIView()
    @objc public let maskImage = UIImageView()
    @objc public let maskImage1 = UIImageView()

    
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.label.textAlignment = .center
        self.label.numberOfLines = 0;
        self.addSubview(self.label)
        
     
        let labelColor = UIColor(red: 35/255.0, green: 57/255.0, blue: 64/255.0, alpha: 1.0)
        self.label.attributedText = NSMutableAttributedString.getAttributed(string: "个人中心", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 35/255.0, green: 57/255.0, blue: 64/255.0, alpha: 1.0))
        
        self.label.textColor = labelColor


     //线
     self.line.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 239/255.0, alpha: 1.0)
     self.addSubview(self.line)

    self.content.backgroundColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    self.addSubview(self.content)

    self.listView.backgroundColor = UIColor(red: 235/255.0, green: 250/255.0, blue: 255.0/255.0, alpha: 1.0)
    self.content.addSubview(self.listView)
     
    //头像
    self.avatar.isUserInteractionEnabled = false
    self.avatar.backgroundColor = UIColor.clear
    self.content.addSubview(self.avatar)
       
    //名字

    self.nickName.isUserInteractionEnabled = false
    self.content.addSubview(self.nickName)
    self.nickName.textColor = UIColor(red: 27/255.0, green: 57/255.0, blue: 71/255.0, alpha: 1.0)
     
    self.nickName.font = UIFont.PingFang(size: 14)
 
    self.nickName.text = "我是测试的名字";
    self.nickName.layer.borderColor = UIColor(red: 235/255.0, green: 239/255.0, blue: 242/255.0, alpha: 1.0).cgColor
       
    // 编辑1
    self.edit.setImage(UIImage.init(named: "HTLive.bundle/edit1"), for: .normal)
      
    self.edit.titleLabel!.font = UIFont.PingFang(size: 18)
    self.edit.setTitleColor(UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1.0), for: .normal)
  
    self.content.addSubview(self.edit)
    self.edit.addTarget(self, action: #selector(self.ceditEvents), for: UIControl.Event.touchUpInside)
    //编辑蒙板
    self.maskImage.isHidden = true
    self.maskImage.image = UIImage.init(named: "HTLive.bundle/editCover")
    self.avatar.addSubview(self.maskImage)
    //编辑蒙板1
    self.maskImage1.image = UIImage.init(named: "HTLive.bundle/editCover1")
    self.maskImage.addSubview(self.maskImage1)
    self.maskImage1.contentMode = .scaleAspectFill
        

    }
    
    @objc func ceditEvents()
    {
        self.ceditSave(save: true)
    }
    @objc func ceditSave(save:Bool)
    {
        

        self.edit.isSelected = !self.edit.isSelected
        
        if (self.edit.isSelected) {
            
            self.tempNickName = self.nickName.text! as NSString;
            self.edit .setImage(UIImage(), for: .normal)
            self.edit.setTitle("保存", for: .normal)
           
            // 设置边框 大小
            self.nickName.layer.borderWidth = 1
           //边框颜色
            self.nickName.isUserInteractionEnabled = true
            self.maskImage.isHidden = false
            self.avatar.isUserInteractionEnabled = true
            self.maskImage.resignFirstResponder()
        }else{
            self.avatar.isUserInteractionEnabled = false
            self.maskImage.isHidden = true
            self.nickName.isUserInteractionEnabled = false
            self.nickName.layer.borderWidth = 0
            self.edit.setImage(UIImage.init(named: "HTLive.bundle/edit1"), for: .normal)
            self.edit.setTitle("", for: .normal)
         
            if ( save == false) {
                self.nickName.text = self.tempNickName as String
            }
        }
        
    }
   @objc public func ceditClick()
    {
        self.ceditSave(save: false)
      
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.label.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 50)
        self.line.frame = CGRect(x: 0, y: 49.5, width: self.frame.size.width, height: 0.5)
        self.content.frame = CGRect(x: 0, y: self.frame.size.height - 110, width: self.frame.size.width, height: 110)
        
        
        self.avatar.frame = CGRect(x: 15, y: (self.content.frame.size.height-50)/2, width: 50, height: 50)
        
        
        self.maskImage.frame = self.avatar.bounds
        self.maskImage1.frame = CGRect(x: (self.maskImage.frame.size.width-24)/2, y: (self.maskImage.frame.size.height-24)/2, width: 24, height: 24)
        
        

        self.avatar.layer.masksToBounds  = true
        // 圆角半径  能把一个下正文体,改成圆形
        self.avatar.layer.cornerRadius = self.avatar.frame.size.height/2
        let nickNameX:CGFloat = 74;
        let nickNameWidth:CGFloat = self.frame.size.width - nickNameX - 79.5;
        
        self.nickName.frame = CGRect(x: nickNameX, y: (self.content.frame.size.height-44)/2, width: nickNameWidth, height: 44)
     
        
        let editX:CGFloat = self.frame.size.width - 79.5
        self.edit.frame = CGRect(x: editX, y: (self.content.frame.size.height-25)/2, width: 79.5, height: 25)
        self.initMask()

        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
